public class Solution111 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    int ans=0;

    public int minDepth(TreeNode root) {
        dfs(1,root);
        return ans;
    }

    private void dfs(int k, TreeNode root) {
        if (root==null){
            return;
        }
        if (root.left==null && root.right==null){
            ans=ans==0?k:Math.min(ans,k);
        }
        dfs(k+1,root.left);
        dfs(k+1,root.right);
    }

    public static void main(String[] args) {
        System.out.println(new Solution111().minDepth(new TreeNode(3,new TreeNode(9),new TreeNode(20,new TreeNode(15),new TreeNode(7)))));
    }
}
